//
//  HETWSymptomCell.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/5/22.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HETWSymptomCell.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "ZQSearchNormalCell.h"

@interface HETWSymptomCell ()<QMUITextViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) QMUITextView *textView;
@property (nonatomic, strong) UIButton *leftLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) CGFloat height;
@end

@implementation HETWSymptomCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.width = KScreenWidth;
         // Fix the bug in iOS7 - initial constraints warning
         self.contentView.bounds = [UIScreen mainScreen].bounds;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _leftLabel = [UIButton new];
    [self.contentView addSubview:_leftLabel];
    _leftLabel.userInteractionEnabled = NO;
    _leftLabel.titleLabel.font = DYBlodFont(18);
    _leftLabel.imageEdgeInsets = UIEdgeInsetsMake(0,-15, 0, 0);
    [_leftLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_leftLabel setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_leftLabel setTitle:@"症状：" forState:UIControlStateNormal];
 
    UILabel *notice = [UILabel new];
    notice.text = @"(您可以点击下方常见症状进行快捷输入)";
    notice.font = DYFont(15);
    notice.textColor = CNavDargGrayColor;
    notice.numberOfLines = 0;
    [self.contentView addSubview:notice];

    _textView = [QMUITextView  new];
    _textView.backgroundColor = CLightGrayColor;
    _textView.layer.masksToBounds = YES;
    _textView.layer.cornerRadius = 6;
    // 限制可输入的字符长度
    self.textView.placeholder = @"请填写备注信息，最多输入500个字!";
    self.textView.placeholderColor = CNavSubTitleColor; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: DYFont(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    [self.contentView addSubview:_textView];
    [_textView setDelegate:self];
    
    
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(20);
        make.left.equalTo(self.contentView).mas_offset(15);
    }];
    
    // 自适应title长度
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_leftLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [notice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_leftLabel).mas_offset(0);
        make.left.equalTo(_leftLabel.mas_right).mas_offset(5);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_leftLabel.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.height.mas_equalTo(150);
    }];
    
    UILabel *symLb = [UILabel new];
    symLb.text = @"常见症状：";
    symLb.font = DYFont(16);
    symLb.textColor = CNavDargGrayColor;
    [self.contentView addSubview:symLb];
    
    [symLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(20);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
 
    UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.alwaysBounceVertical = YES;
    [self.contentView addSubview:_collectionView];
    [self.collectionView registerClass:[ZQSearchNormalCell class] forCellWithReuseIdentifier: @"ZQSearchNormalCell"];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(symLb.mas_bottom).mas_offset(5);
        make.left.equalTo(self.contentView).mas_offset(20);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.height.mas_equalTo(50).priorityLow();//设置一个高度，以便赋值后更新
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {

    self.fiterModel = fiterModel;
    [_leftLabel setTitle:fiterModel.leftTitle forState:UIControlStateNormal];
    [_leftLabel setImage:(fiterModel.isHasMustSigns?[UIImage imageNamed:@"3"]: nil) forState:UIControlStateNormal];
    
    _textView.placeholder = fiterModel.placeHolder;
    _textView.text = [formModel valueForKey:fiterModel.tableKey];
    
    
    [_collectionView reloadData];
    /** 强制视图立即更新其布局，即同步执行 立即获取collectionView的高度*/
    [_collectionView layoutIfNeeded];
    
    //获取collectionview高度
    //更新collectionView
    _height = [fiterModel getCollectionHeight: _collectionView];
    [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(_height).priorityMedium();
    }];
}
 
#pragma mark - <QMUITextViewDelegate>
// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    
}

- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [Dialog toast:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] delay:2.0];
}
 
- (void)textViewDidChange:(UITextView *)textView {
    [self.formModel setValue:textView.text forKey:self.fiterModel.tableKey]; // 将textField中的值赋给_formModel
}



#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSArray *secs = self.fiterModel.cellData;
    return secs.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[section];
    return rows.count;
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZQSearchNormalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZQSearchNormalCell" forIndexPath:indexPath];
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[indexPath.section];
    cell.title = rows[indexPath.item];

    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[indexPath.section];
    NSString *item = rows[indexPath.item];
    
    NSString *specStr = indexPath.section == 0 ? @"" : @"，";
    
    NSString *string = [NSString stringWithFormat:@"%@%@%@", _textView.text, item, specStr];
    _textView.text = string;
    [self.formModel setValue:_textView.text forKey:self.fiterModel.tableKey]; // 将textField中的值赋给_formModel
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[indexPath.section];
    NSString *string = rows[indexPath.item];
    
    CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options: NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: DYFont(16)} context:nil].size;
    
    CGFloat width = size.width + 15 * 2;
    
    if (width >= (KScreenWidth-40)) {
        width = KScreenWidth-40;
    }
    
    CGSize rSize = CGSizeMake(width, 34);
    
    return rSize;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(15, 15, 0, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10.f;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, 10);
}


@end
